//
//  GCRegistVC.m
//  CBHGroupCar
//
//  Created by mac on 2018/2/1.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYRegisterVC.h"
#import "Head.h"
#import "GYFinishRegisterVC.h"
#import "GYRegisterProtocolVC.h"
#define unshowImage           [UIImage imageNamed:@"browse_fill"]
#define showImage             [UIImage imageNamed:@"browse_fill (1)"]
@interface GYRegisterVC ()<UITextFieldDelegate>
@property (nonatomic,strong) UIView *inviteFootV;
@property (nonatomic,strong) UIButton *sendBTN;
@property (nonatomic,strong) UIButton *showPswBtn;
@property (nonatomic,strong)UIButton *registButton;
@property (nonatomic,strong)UIButton *noticeBtn;
@property (nonatomic,strong)UILabel *noticeLab;
@property(nonatomic,strong)GYLoginViewModel* loginViewModel;

@end

@implementation GYRegisterVC
#pragma mark - getter and setter

-(GYLoginViewModel *)loginViewModel
{
    if (!_loginViewModel) {
        _loginViewModel=[GYLoginViewModel shareInterface];
    }
    return _loginViewModel;
}
-(UIButton *)showPswBtn
{
    if (!_showPswBtn) {
        _showPswBtn = [[UIButton alloc]init];
        _showPswBtn.frame =  WFCGRectMake(320, (52-unshowImage.size.width)/2, unshowImage.size.width, unshowImage.size.height);
        [_showPswBtn setBackgroundImage:unshowImage forState:UIControlStateNormal];
        [_showPswBtn setBackgroundImage:showImage forState:UIControlStateSelected];
        [_showPswBtn addTarget:self action:@selector(shwoPsw:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _showPswBtn;
}
-(GYRegiserViewModel *)regist
{
    if (!_regist) {
        _regist = [[GYRegiserViewModel alloc] init];
        _regist.registerType=self.isForget?@"2":@"1";
    }
    return _regist;
}
-(UIView *)inviteFootV
{
    if (!_inviteFootV) {
        _inviteFootV = [UIView new];
        _inviteFootV.frame = CGRectMake(0, 0, self.view.width, 300);
        if (!_isForget)
        {
            [_inviteFootV addSubview:self.noticeBtn];
            [_inviteFootV addSubview:self.noticeLab];
        }

        [_inviteFootV addSubview:self.registButton];
    }
    return _inviteFootV;
}
-(UIButton *)registButton
{
    if (!_registButton)
    {
        _registButton = [[UIButton alloc]init];
        [_registButton setBackgroundColor:Main_Color_LightRed forState:UIControlStateNormal];
        [_registButton setBackgroundColor:Main_Color_Red forState:UIControlStateSelected];
        [_registButton setFrame:_isForget?WFCGRectMake(20, 20, 335, 45):WFCGRectMake(20, 56, 335, 45)];
        
        [_registButton setTitle:_isForget?@"完成":@"立即注册" forState:UIControlStateNormal];
        [WFFunctions WFUIaddbordertoView:_registButton radius:WFCGFloatY(4) width:0 color:nil];
        [_registButton addTarget:self action:@selector(rigistButtonTap:) forControlEvents:UIControlEventTouchDown];
    }
    return _registButton;
}
-(UIButton *)noticeBtn
{
    if (!_noticeBtn)
    {
        _noticeBtn = [[UIButton alloc]init];
        [_noticeBtn setBackgroundImage:[UIImage imageNamed:@"bty"] forState:UIControlStateNormal];
         [_noticeBtn setBackgroundImage:[UIImage imageNamed:@"Checkbox"] forState:UIControlStateSelected];
        [_noticeBtn setFrame:WFCGRectMake(84, 25, 16, 16)];
        _noticeBtn.selected=YES;
        [_noticeBtn addTarget:self action:@selector(selectNoticePress:) forControlEvents:UIControlEventTouchDown];
    }
    return _noticeBtn;
}
-(UILabel *)noticeLab
{
    if (!_noticeLab) {
        _noticeLab=[[UILabel alloc] init];
        _noticeLab.frame=WFCGRectMake(110, 25, 200, 16);
        _noticeLab.font=SYSTEMFONT(13);
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"我已经阅读并同意《注册协议》" attributes:@{
                                                                                                                                       NSFontAttributeName:SYSTEMFONT(13),
                                                                                                                                       NSForegroundColorAttributeName: [UIColor colorWithWhite:102.0f / 255.0f alpha:1.0f]
                                                                                                                                       }];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:74.0f / 255.0f green:144.0f / 255.0f blue:226.0f / 255.0f alpha:1.0f] range:NSMakeRange(8, 6)];
        
        _noticeLab.attributedText=attributedString;
        _noticeLab.userInteractionEnabled=YES;
        
        UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoNoticeWeb)];
        tap.numberOfTapsRequired=1;
        [_noticeLab addGestureRecognizer:tap];
    
    }
    return _noticeLab;
}
-(UIButton *)sendBTN
{
    if (!_sendBTN) {
        _sendBTN = [[UIButton alloc]init];
        _sendBTN.frame = WFCGRectMake(375 - 118, 11, 98, 30);
        [_sendBTN setBackgroundColor:RGBACOLOR(184, 26, 8, 0.5) forState:UIControlStateNormal];
        [_sendBTN setTitle:_regist.validateCodeStatus forState:UIControlStateNormal];
        [_sendBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sendBTN.titleLabel.font = SYSTEMFONT(13);
        [WFFunctions WFUIaddBorderToView:_sendBTN top:NO left:YES bottom:NO right:YES borderColor:Main_Color_BG borderWidth:0.5f];
        [_sendBTN addTarget:self action:@selector(sendCodeTap:) forControlEvents:UIControlEventTouchUpInside];
        _sendBTN.enabled = _regist.canSendCode;
    }
    return _sendBTN;
}
#pragma mark - event
-(void)rigistButtonTap:(UIButton*)sender
{
    [self.view endEditing:YES];
    if (sender.selected)
    {
        [self.regist requestRegister];
    }
}
-(void)selectNoticePress:(UIButton*)sender
{
    sender.selected=!sender.selected;

    if (_regist.password.length>=6&&_regist.registPhone.length==11&&_regist.validateCode.length>0&&sender.selected==YES)
    {
        _registButton.selected=YES;
    }else
    {
        _registButton.selected=NO;
    }
}
-(void)gotoNoticeWeb
{
    GYRegisterProtocolVC* gy=[[GYRegisterProtocolVC alloc] init];
    [self.navigationController pushViewController:gy animated:YES];
}
-(void)sendCodeTap:(id)sender
{
     [self.regist sendCode];
}
#pragma mark - method
#pragma mark - method

-(void)login
{
    self.loginViewModel.username=self.regist.registPhone;
    self.loginViewModel.password=self.regist.password;
    [self.loginViewModel requsetLogin];
}
-(void)shwoPsw:(UIButton*)sender
{
    UITableViewCell* cell=[self.baseTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    UITextField* tf=(UITextField*)[cell.contentView viewWithTag:2];
    tf.secureTextEntry=sender.selected;
    sender.selected=!sender.selected;
}
-(void)initilization
{
    
    self.navigationItem.title = _isForget?@"忘记密码":@"注册";
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    
    [self.baseTable setTableFooterView:self.inviteFootV];

    [self setNavgationTheme:1];
    [self setOtherBackImage:@"backicon_black"];
    [self viewModelBind];

}
-(void)viewModelBind
{
    [self.regist addObserver:self forKeyPath:@"validateCodeStatus" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [self.regist addObserver:self forKeyPath:@"success" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
#pragma mark - life
-(void)dealloc
{
    [self.regist removeObserver:self forKeyPath:@"validateCodeStatus"];
    [self.regist removeObserver:self forKeyPath:@"success"];
    [self.regist invalidateTimer];
    self.regist=nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    
    if ([keyPath isEqualToString:@"validateCodeStatus"])
    {
        [_sendBTN setTitle:_regist.validateCodeStatus forState:UIControlStateNormal];
        _sendBTN.enabled = _regist.canSendCode;
        [_sendBTN setBackgroundColor:!_regist.canSendCode?Main_Color_Red:RGBACOLOR(184, 26, 8, 0.5)];
    }
    
    if ([keyPath isEqualToString:@"success"])
    {
        if (self.regist.success==YES)
        {
            if (_isForget)
            {
                [self.navigationController popViewControllerAnimated:YES];
            }else
            {
                GYFinishRegisterVC* gg=[[GYFinishRegisterVC alloc] init];
                HMSNavigationController* nav=[[HMSNavigationController alloc] initWithRootViewController:gg];
               // [self presentViewController:nav animated:YES completion:nil];
                [UIApplication sharedApplication].delegate.window.rootViewController=nav;
            }
        }
    }
}
#pragma mark - delegate
#pragma mark - table delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _isForget?3:4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray* imageArr=@[@"mobilephone_fill",@"lock_fill",@"yzm",@"zc_yqm"];
    static NSString *identify = @"section2cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView removeAllSubviews];
    
    UIImageView* leftIV=[[UIImageView alloc] init];
    leftIV.image=[UIImage imageNamed:imageArr[indexPath.row]];
    [cell.contentView addSubview:leftIV];
    leftIV.frame=WFCGRectMake(27, (52-leftIV.image.size.height)/2, leftIV.image.size.width, leftIV.image.size.height);
    
    UILabel *titlelb = [[UILabel alloc]init];
    titlelb.frame = WFCGRectMake(54, 0, 50, 52);
    titlelb.font = SYSTEMFONT(15);
    titlelb.textColor = Main_Color_Black;
    [cell.contentView addSubview:titlelb];
    
    UITextField *contenttf = [[UITextField alloc]init];
    contenttf.frame = WFCGRectMake(110, 0,200, 52);
    [contenttf setTextColor:Main_Color_Black];
    contenttf.font = SYSTEMFONT(15);
    contenttf.delegate = self;
    contenttf.tag = indexPath.row;
    contenttf.keyboardType = UIKeyboardTypeDefault;
   // contenttf.clearButtonMode=UITextFieldViewModeWhileEditing;
    [cell.contentView addSubview:contenttf];
    
    if (3 == indexPath.row) {
        titlelb.text = @"邀请码";
        contenttf.text = _regist.inviteInfo;
        contenttf.placeholder = @"(选填)";
        //contenttf.enabled = NO;
        contenttf.secureTextEntry = NO;
        contenttf.keyboardType = UIKeyboardTypePhonePad;
    }else if (0 == indexPath.row){
        titlelb.text = @"手机号";
        contenttf.text = _regist.registPhone;
        contenttf.placeholder = @"11位手机号";
        contenttf.enabled = YES;
        contenttf.secureTextEntry = NO;
        contenttf.keyboardType = UIKeyboardTypePhonePad;
    }else if (1 == indexPath.row) {
        contenttf.frame = WFCGRectMake(110, 0, 120, 52);
        titlelb.text = @"验证码";
        contenttf.text = _regist.validateCode;
        contenttf.placeholder = @"验证码";
        contenttf.enabled = YES;
        contenttf.secureTextEntry = NO;
        contenttf.keyboardType = UIKeyboardTypeNumberPad;
        [cell.contentView addSubview:self.sendBTN];
        
    }else{
        titlelb.text = @"密  码";
        contenttf.text = _regist.password;
        contenttf.placeholder = @"设置登录密码,至少六位";
        contenttf.enabled = YES;
        contenttf.secureTextEntry = YES;
        [cell.contentView addSubview:self.showPswBtn];
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WFCGFloatY(52);
}

#pragma mark - text field delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString* allStr=[NSString stringWithFormat:@"%@%@",textField.text,string];
    if (0 == textField.tag) {
        _regist.registPhone=allStr;
    }
    if (1 == textField.tag) {
        _regist.validateCode =allStr;
    }
    if (2 == textField.tag) {

         _regist.password =allStr;
    }
    if (3 == textField.tag) {
        _regist.inviteInfo = allStr;
    }
    if (_isForget)
    {
        if (_regist.password.length>=6&&_regist.registPhone.length==11&&_regist.validateCode.length>0)
        {
            _registButton.selected=YES;
        }else
        {
            _registButton.selected=NO;
        }
    }else
    {
        if (_regist.password.length>=6&&_regist.registPhone.length==11&&_regist.validateCode.length>0&&_noticeBtn.selected==YES)
        {
            _registButton.selected=YES;
        }else
        {
            _registButton.selected=NO;
        }
    }

    
    return YES;
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */

